import os
import re
import threading

import numpy as np
import time
from commen.log import Logger
import openpyxl
import commen.config
def ImuDatacontinuity():
    #---------读取config文件获取筛选字段内容--------------------------
    mcukey = commen.config.global_config.getRaw('keywords', 'mcurtk')
    cankey = commen.config.global_config.getRaw('keywords', 'canrtk')
    # ---------读取config文件获取筛选字段内容--------------------------
    #-----------------------创建excel--------------------------------------------
    # 获取当前时间
    time_now = time.strftime("%Y%m%d-%H%M", time.localtime())
    # 获取当前路径
    result_root = os.getcwd()
    root = os.getcwd()
    result_path = result_root + r'\result' + "\\" + time_now
    log_path = root + r'\original-logfile' + '\\'
    # -----遍历对比log路径获取要对比的文件名路径信息----------------------
    for root, dirs, files in os.walk(log_path):
        for file in files:
            if "can" in file:
                aa = os.path.join(root, file)
            if "mcu" in file:
                bb = os.path.join(root, file)
    # -----遍历对比log路径获取要对比的文件名路径信息----------------------
    book = openpyxl.Workbook()  # 打开一个将写的文件
    sheet = book.create_sheet(index=0)  # 在将写的文件创建sheet
    saveExcel = result_path+"can到mcu的rtk数据完整性验证结果.xlsx"
    sheet.cell(1, 2).value = "mcurtk数据系统时间"
    sheet.cell(1, 3).value = "mcurtk数据"
    sheet.cell(1, 4).value = "canrtk数据"
    sheet.cell(1, 5).value = "是否完整"
    mcurtklist = []
    canrtklist = []
    mcurtksystemtimelist = []

    row =1
    #-----------------------创建excel--------------------------------------------

    logger  = Logger(logger='TestMylog').getlog()
    try:
        canfm = open(aa, 'r', encoding='UTF-8')  # 打开目标文件
        mcufm = open(bb, 'r', encoding='UTF-8') # 打开目标文件
    except:
        canfm = open(aa, 'r', encoding='gbk')  # 打开目标文件
        mcufm = open(bb, 'r', encoding='gbk')  # 打开目标文件
    logger.info("打开目标文件")
    mculines = mcufm.readlines()
    canlines = canfm.readlines()
    logger.info("获取内容")
    logger.info("开始逐行读取")
    for rtkline in mculines:
        if mcukey in rtkline:
            mcurtksystemtime = rtkline[:rtkline.index("<thread")]
            mcurtkold = rtkline.split("+0:")
            mcurtk = mcurtkold[1].replace("+10:","")
            mcurtk = mcurtk.replace("+20:", "")
            mcurtk = mcurtk.replace("+30:", "")
            mcurtk = mcurtk.replace(")", "")
            mcurtk = mcurtk.replace("-", "")
            mcurtk = mcurtk.replace(" ", "")
            mcurtk1 = mcurtk[6:22]
            mcurtklist.append(mcurtk1)
            mcurtksystemtimelist.append(mcurtksystemtime)
            mcurtk2 = mcurtk[22:38]
            mcurtklist.append(mcurtk2)
            mcurtksystemtimelist.append(mcurtksystemtime)
            mcurtk3 = mcurtk[38:54]
            mcurtklist.append(mcurtk3)
            mcurtksystemtimelist.append(mcurtksystemtime)
            mcurtk4 = mcurtk[54:70]
            mcurtklist.append(mcurtk4)
            mcurtksystemtimelist.append(mcurtksystemtime)
    for canrtk in canlines:
        if cankey in canrtk:
            canrtkold = canrtk.split("s 8")
            rtkmessage = canrtkold[1].replace(" ","")
            canrtklist.append(rtkmessage.rstrip())
    number = 0
    logger.info("开始进行对比")
    for i in range(len(mcurtklist)):
        row =row+1
        if int(i)+3 == len(mcurtklist):  # 执行到最后一组四包数据时跳出循环
            break
        elif mcurtklist[number] in canrtklist:
            mcurtknumber = canrtklist.index(str(mcurtklist[number]))
            mcurtkmessag = mcurtklist[number]+mcurtklist[int(number)+1]+mcurtklist[int(number)+2]+mcurtklist[int(number)+3]
            canrtkmessage = canrtklist[mcurtknumber]+canrtklist[int(mcurtknumber)+1]+canrtklist[int(mcurtknumber)+2]+canrtklist[int(mcurtknumber)+3]
            logger.info("正在对比："+"MCU上"+mcurtkmessag+"数据与CAN上"+canrtkmessage+"数据是否一致")
            if mcurtkmessag == canrtkmessage:
                sheet.cell(row, 2).value = mcurtksystemtimelist[number]
                sheet.cell(row, 3).value = mcurtkmessag
                sheet.cell(row, 4).value = canrtkmessage
                sheet.cell(row, 5).value = "是"
                # canrtklist[mcurtknumber] = "ppp"

            else:
                sheet.cell(row, 2).value = mcurtksystemtimelist[number]
                sheet.cell(row, 3).value = mcurtkmessag
                sheet.cell(row, 4).value = canrtkmessage
                sheet.cell(row, 5).value = "否"
            number = int(i) + 4
        else:
            sheet.cell(row, 2).value = mcurtksystemtimelist[number]
            sheet.cell(row, 3).value = mcurtkmessag
            sheet.cell(row, 5).value = "未找到数据"
            number = int(i) + 4











    book.save(saveExcel)

ImuDatacontinuity()